package com.creativestar.elixurecrm.server;

import java.io.IOException;
import java.util.List;

import javax.xml.bind.JAXBContext;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import com.creativestar.elixurecrm.company.Qdbapi;
import com.creativestar.elixurecrm.domain.Company;
import com.creativestar.elixurecrm.info.ElixurecrmInfo;

@SuppressWarnings({ "resource", "deprecation" })
public class ElixurecrmInfoLoader {

	public static void main(String[] args) throws Exception, IOException {
		
		String	rid ="2918";
		
		Company company = getCompany(String.valueOf(rid));
		postCompany(company,rid);
	}

	private static void postCompany(Company company,String rid) {
		
		HttpClient httpClient = new DefaultHttpClient();
		try {
			String url = ElixurecrmInfo.BASE_API_URL+ ElixurecrmInfo.DBID_OPPORTUNITIES+"?act=API_AddRecord"+ ElixurecrmInfo.USER_AUTH_STRING;
			
			HttpPost request = new HttpPost(url);
			StringEntity params = new StringEntity(getXml(company,rid));
			request.setHeader("content-type", "application/xml");
			request.setEntity(params);

			HttpResponse response = httpClient.execute(request);
			System.out.println("rid:"+rid+"    status:   "+response.getStatusLine().toString());
		} catch (Exception ex) {
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
	}

	private static Company getCompany(String rid) throws Exception {

		String url = ElixurecrmInfo.getRecordInfoUrl(
				ElixurecrmInfo.DBID_COMPANIES, rid);

		HttpClient client = HttpClientBuilder.create().build();
		HttpPost post = new HttpPost(url);

		HttpResponse response = client.execute(post);
		JAXBContext context = JAXBContext.newInstance(Qdbapi.class);
		Qdbapi qdbapi = (Qdbapi) context.createUnmarshaller().unmarshal(response.getEntity().getContent());

		List<Qdbapi.Field> fieldList = qdbapi.getField();

		Company company = new Company();

		for (Qdbapi.Field f : fieldList) {
			switch (f.getFid()) {
			case ElixurecrmInfo.ACCOUNTMANAGER_FID:
				company.setAccountManager(f.getValue());
				break;

			case ElixurecrmInfo.ADDRESS_FID:
				company.setAddress(f.getValue());
				break;
			case ElixurecrmInfo.COMPANYNAME_FID:
				company.setCompanyName(f.getValue());
				break;
			case ElixurecrmInfo.ELIXURESTATUS_FID:
				company.setElixureStatus(f.getValue());
				break;

			case ElixurecrmInfo.EMAILLADDRESS_FID:
				company.setEmaillAddress(f.getValue());
				break;

			case ElixurecrmInfo.NOTES_FID:
				company.setNotes(f.getValue());
				break;

			case ElixurecrmInfo.PHONE_FID:
				company.setPhone(f.getValue());
				break;

			case ElixurecrmInfo.WEB_FID:
				company.setWeb(f.getValue());
				break;
			default:
				break;
			}
		}
		return company;
	}
	
	private static String getXml(Company company,String rid){
		StringBuffer str = new StringBuffer();
		str.append("<qdbapi>");
		str.append("<ticket>"+ElixurecrmInfo.TICKET+"</ticket>");
		str.append("<apptoken>"+ElixurecrmInfo.APPTOKEN+"</apptoken>");
		str.append("<field fid='32'>"+rid+"</field>");
		str.append("<field fid='10'>"+"Digital Marketing - China"+"</field>");
		str.append("<field fid='21'>"+company.getAccountManager()+"</field>");
		str.append("<field fid='14'>"+"O1 - New"+"</field>");
		str.append("</qdbapi>");
		return str.toString();
	}
}
